class Solution
{
public:
    int countServers(vector<vector<int>> &grid)
    {
        int m = grid.size();
        int n = grid[0].size();
        vector<int> rowSum(m, 0);
        vector<int> colSum(n, 0);
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 1)
                {
                    ++rowSum[i];
                    ++colSum[j];
                }
            }
        }

        int count = 0;
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 1 && (rowSum[i] > 1 || colSum[j] > 1))
                {
                    ++count;
                }
            }
        }
        return count;
    }
};